## ITANES
require(memisc)
itanes_2013 <- as.data.frame(as.data.set(spss.system.file("ITA2013_(envers2015_01_19).sav"))) # Not provided
itanes_voted_m5s_2013 <- subset(itanes_2013, d89=='M5S - Five Stars Movement (leader: Grillo)')

df <- subset(itanes_voted_m5s_2013, !(d45 %in% c('Other','Didn\'t vote','Don\'t remember','No answer')))
df$d45 <- as.character(df$d45)

df <- as.data.frame(prop.table(table(df$d45)))

df$Var1 <- factor(df$Var1, levels=c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                    'IdV - Italy of Values (leader: Di Pietro)',
                                    'PD -  Democratic Party (leader: Veltroni)',
                                    'PS - Socialist Party (leader: Boselli)',
                                    'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                    'PDL - The People of Freedom (leader: Berlusconi)',
                                    'LN - Northern League (leader: Bossi)'))
df$source <- 'Provenance of M5S votes casted in 2013 relatively to 2008 election'

m5s_vote_prov <- df # to use later

df <- subset(itanes_voted_m5s_2013, d72_5=="Every day" |
               d72_5=="A few times a week" |
               d72_5=="From time to time")

n <- nrow(df)

df <- subset(df, !(d45 %in% c('Other','Didn\'t vote','Don\'t remember','No answer')))
df$d45 <- as.character(df$d45)

df <- as.data.frame(prop.table(table(df$d45)))

df$Var1 <- factor(df$Var1, levels=c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                    'IdV - Italy of Values (leader: Di Pietro)',
                                    'PD -  Democratic Party (leader: Veltroni)',
                                    'PS - Socialist Party (leader: Boselli)',
                                    'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                    'PDL - The People of Freedom (leader: Berlusconi)',
                                    'LN - Northern League (leader: Bossi)'))

df <- rbind(df, data.frame(Var1 = 'PS - Socialist Party (leader: Boselli)',
                           Freq = 0))
df <- rbind(df, data.frame(Var1 = 'DC - Union of Christian and Centre Democrats (leader: Casini)',
                           Freq = 0))

right_vote_perc_online <- sum(df$Freq[grepl('^DC -|^LN -|^PDL -',df$Var1)])
left_vote_perc_online <- sum(df$Freq[grepl('^PD -|^Sinistra|^PS -|^IdV -',df$Var1)])

df$source <- 'Voted M5S in 2013 and active online'

m5s_vote_prov$source <- 'Voted M5S in 2013'

df <- merge(df, m5s_vote_prov, by='Var1')

df$diff <- df$Freq.x - df$Freq.y

p1 <- ggplot(data=df, aes(x=Var1, y=Freq.x)) + 
  geom_bar(stat='identity') +
  geom_text(aes(label=round(Freq.x*100,digits=2)), 
            vjust=ifelse(df$Freq.x >= 0, -0.1 , 1.1), size=3) +
  scale_y_continuous(labels=scales::percent) +
  scale_x_discrete(labels=c("SA","IdV","PD","PS","DC","PDL","LN"), drop=FALSE) +
  labs(x=(paste0('n=',n)), y=NULL)

p2 <- ggplot(data=df, aes(x=Var1, y=diff)) + 
  geom_bar(stat='identity') +
  geom_text(aes(label=round(diff*100,digits=2)), 
            vjust=ifelse(df$diff >= 0, -0.1 , 1.1), size=3) +
  scale_y_continuous(labels=scales::percent) +
  scale_x_discrete(labels=c("SA","IdV","PD","PS","DC","PDL","LN"), drop=FALSE) +
  labs(x=(paste0('n=',n)), y=NULL)

grid.arrange(p1,p2,nrow=1)